// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences:{
      preload: path.join(__dirname,'preload.js')
    }
  })

  // and load the index.html of the app.
  // mainWindow.loadFile('index.html')
  // mainWindow.loadURL('http://rongdai.yxdian.com:8080')
  // mainWindow.loadURL('https://www.baidu.com/');
  console.log('------- load file -------',path.resolve(__dirname,'index.html'));
  mainWindow.loadFile(path.resolve(__dirname,'index.html'))
  mainWindow.webContents.openDevTools();

  // Show window when page is ready
  mainWindow.once('ready-to-show', () => {
    mainWindow.maximize()
    mainWindow.show()
  })

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// 应用加载完后开始渲染窗口
app.whenReady().then(() => {
  createWindow()

  //如果没有窗口则打开一个新窗口
  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// 关闭所有窗口时退出应用
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})
